define(['template', 'request', 'jquery'], (template, req) => {
    class Header {
        constructor() {
            this.init();
            this.index = 0;
            this.num = 0;
            this.allCount = 0;
        }
        init() {
            $('header').load('/html/header.html', () => {
                this.computedCount();
                this.welcome();
                this.exit();
            });
            this.getNavData();
        }
        getNavData() {
            req.getNavData()
                .then(res => {
                    this.render(res.data.navs);
                })
        }
        //渲染页面
        render(list) {
            let index = this.index;
            let num = this.num;
            $(".main > .list > .Lists").html(template("h-botTemplate", { list, index, num }));
            $(".menu .main").click(function () {
                let Index = $(this).parent().parent().index();
                const productId = list[Index].subs[$(this).index()].link.split('/')[5];
                location.href = `/html/details.html?${productId}`;
            })
        }
        // 计算数量
        computedCount() {
            this.allCount = 0
            // 先把 localStorage 里面的数据取出来
            let prodList = localStorage.getItem('prodList')
            if (prodList) {
                prodList = JSON.parse(prodList)
                prodList.forEach(item => {
                    this.allCount += item.count
                })
            }
            $('.t-right .cart .num').html(this.allCount)
        }
        welcome() {
            const user = localStorage.getItem("user");
            const user1 = JSON.parse(user);
            user1.forEach(item => {
                if(item.status === 200){
                    $(".t-right .login").addClass('hide').siblings().removeClass('hide');
                    $(".t-right .welcome .name").text(item.name);
                }
            })
        }
        exit() {
            $(".t-right .welcome .ex").click(() => {
                const user = localStorage.getItem("user");
                const user1 = JSON.parse(user);
                user1.forEach(item => {
                    if(item.status === 200){
                        $(".t-right .welcome").addClass('hide').siblings().removeClass('hide');
                        item.status = 0;                       
                        localStorage.setItem('user',JSON.stringify(user1));
                        alert("注销成功")
                    }
                })
            })
        }
    }
    return new Header();
})